package com.sunyur.tailor.channel.web.outer.invocie.controller;

import com.alibaba.fastjson.JSON;
import com.sunyur.tailor.channel.common.middle.base.ChannelResult;
import com.sunyur.tailor.channel.common.middle.enums.MiddleResultEnum;
import com.sunyur.tailor.channel.service.inner.invoice.param.InvoiceReq;
import com.sunyur.tailor.channel.service.inner.invoice.param.PaybackParam;
import com.sunyur.tailor.channel.service.inner.invoice.service.SunyurInvoiceService;
import com.sunyur.tailor.channel.web.outer.invocie.dto.InvoiceDTO;
import com.sunyur.tailor.channel.web.outer.invocie.dto.PaybackDTO;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @Author pengbing
 * @Description //结算发票控制层
 * @Date 11:31 2021/6/8
 * @Param
 * @return
 **/
@Log4j2
@RestController
@RequestMapping("/outer/invoice")
public class InvoiceController {
    /**
     * 结算发票服务
     */
    @Autowired
    private SunyurInvoiceService sunyurInvoiceService;

    /**
     * 预制发票财务审核
     */
    @PostMapping(value = "/confirm", produces = MediaType.APPLICATION_JSON_VALUE)
    public ChannelResult confirm(@RequestBody @Valid InvoiceDTO invoiceDTO) {
        log.info("InvoiceController confirm param:{}", JSON.toJSONString(invoiceDTO));
        ChannelResult result = new ChannelResult<>();
        try {
            InvoiceReq invoiceReq = new InvoiceReq();
            BeanUtils.copyProperties(invoiceDTO,invoiceReq);
            result = sunyurInvoiceService.singleConfirm(invoiceReq);
        } catch (Exception e) {
            log.error("InvoiceController confirm exception:{}", e);
            result.setError(MiddleResultEnum.EXCEPTION.getCode(), MiddleResultEnum.EXCEPTION.getValue());
        }
        return result;
    }

    /**
     * 支付回传，先需回传付款申请，再支付完成付款申请
     */
    @PostMapping(value = "/payback", produces = MediaType.APPLICATION_JSON_VALUE)
    public ChannelResult confirm(@RequestBody @Valid PaybackDTO paybackDTO) {
        log.info("InvoiceController payback param:{}", JSON.toJSONString(paybackDTO));
        ChannelResult result = new ChannelResult<>();
        try {
            PaybackParam paybackParam = new PaybackParam();
            BeanUtils.copyProperties(paybackDTO,paybackParam);
            result = sunyurInvoiceService.payback(paybackParam);
        } catch (Exception e) {
            log.error("InvoiceController payback exception:{}", e);
            result.setError(MiddleResultEnum.EXCEPTION.getCode(), MiddleResultEnum.EXCEPTION.getValue());
        }
        return result;
    }
}